import { Module } from 'vuex'
interface SystemState {
  deviceInfo: any
}

const systemModule: Module<SystemState, any> = {
  namespaced: true,
  state: () => ({
    deviceInfo: uni.getStorageSync('deviceInfo') || null
  }),
  mutations: {
    setDeviceInfo(state, payload) {
      state.deviceInfo = payload
      uni.setStorageSync('deviceInfo', payload)
    }
  },
  actions: {
    async getDeviceInfo({ commit }) {
      try {
        // 如果已有缓存则直接返回
        if (uni.getStorageSync('deviceInfo')) {
          return
        }
        const info = await uni.getSystemInfo()
        commit('setDeviceInfo', info)
      } catch (error) {
        console.error('获取设备信息失败:', error)
      }
    }
  },
  getters: {
    statusBarHeight: (state) => {
      return state.deviceInfo?.statusBarHeight || 0
    },
    bottomSafeArea: (state) => {
      return state.deviceInfo?.safeAreaInsets?.bottom || 0
    }
  }
}

export default systemModule
